/**
 * 
 */
package com.bitBusy.manageyourriches.utilityClasses.adapters;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bitBusy.manageyourriches.R;
import com.bitBusy.manageyourriches.database.contactClasses.PaymentMethod;
import com.bitBusy.manageyourriches.database.contactClasses.SpendingCategory;
import com.bitBusy.manageyourriches.database.contactClasses.SpendingSubcategory;

/**
 * @author SumaHarsha
 * 
 */
public class BarPlotListAdapter extends BaseAdapter {

	/** time image*/
	private static final int myTimeImg = R.drawable.calendar_clock;
	
	private static final String MYDOLLARSIGN = "$";
	/** max amount*/
	private double myMaxAmount = 1;
	
	private Context myContext;
	
	private LinkedHashMap<?, Double> myDetailsMap;
	
	
	/** keyset array*/
	ArrayList<?> myMapKeys;
	
	public BarPlotListAdapter(Context the_context, LinkedHashMap<?, Double> the_map) {
		myContext = the_context;
		myDetailsMap = the_map;
		if (the_map != null)
		{
			myMapKeys = new ArrayList<Object>(the_map.keySet());
			if (the_map.values().size() > 0)
			{
				myMaxAmount = Collections.max(the_map.values());
			}
		}
	}


	public int getCount() {
		return (myDetailsMap==null?0:myDetailsMap.size());
	}

	public HashMap<Object, Double> getItem(int position) {
		if (myDetailsMap != null)
		{
			HashMap<Object, Double> returnMap = new HashMap<Object, Double>();
			Object key = myMapKeys.get(position);		
			if (key != null)
			{
				returnMap.put(key, myDetailsMap.get(key));
				return returnMap;
			}
		}
		return null;
	}

	public long getItemId(int position) {
			return getCount();
	}

	public View getView(final int position, View the_convertView, ViewGroup parent) {
		
		  if (the_convertView == null) {
		   LayoutInflater inf = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		   the_convertView = inf.inflate(R.layout.viewgraph_barplotlayout, null);		  }
	
		Object key = myMapKeys.get(position);
		
		if (key != null)
		{
			if (key instanceof SpendingCategory)
			{
				SpendingCategory category = (SpendingCategory) key;
				setView(the_convertView, category.getName(), category.getPicID(), myDetailsMap.get(category));
			}
			else if (key instanceof SpendingSubcategory)
			{
				SpendingSubcategory subcategory = (SpendingSubcategory) key;
				setView(the_convertView, subcategory.getName(), subcategory.getImageID(), myDetailsMap.get(subcategory));
			}
			else if (key instanceof PaymentMethod)
			{
				PaymentMethod method = (PaymentMethod) key;
				setView(the_convertView, method.getName(), method.getImageId(), myDetailsMap.get(method));
			}
			else if (key instanceof String)
			{
				String desc = key.toString();
				setView(the_convertView, desc, myTimeImg, myDetailsMap.get(desc));
			}
	
		}
		return the_convertView;
	}


	/**
	 * Sets subviews
	 * @param the_convertView 
	 * @param name
	 * @param picID
	 * @param double1
	 */
	private void setView(View the_convertView, String the_name, int the_imgID, Double the_amount) {
		
		ImageView imgTV = (ImageView) the_convertView.findViewById(R.id.barPlot_catImg);
		imgTV.setImageResource(the_imgID);

		TextView descTV = (TextView) the_convertView.findViewById(R.id.barPlot_catText);
		descTV.setText(the_name);
		
	/*	if (myPreviousView != null)
		{
			LinearLayout currentImgText = (LinearLayout) the_convertView.findViewById(R.id.barPlot_catDetails_imgText);
			LinearLayout prevImgText = (LinearLayout) myPreviousView.findViewById(R.id.barPlot_catDetails_imgText);
			RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			param.addRule(RelativeLayout.al, prevImgText.getRight());
			currentImgText.setLayoutParams(param);
		}*/
		
		TextView barTV = (TextView) the_convertView.findViewById(R.id.barPlot_amountBarTextBox);
		int width =  (int) (the_amount.doubleValue()/myMaxAmount) * 50;
		barTV.setWidth((width == 0 && the_amount > 9)? 25:width); 

		
		TextView amtTV = (TextView) the_convertView.findViewById(R.id.barPlot_amountTextBox);
		amtTV.setText(MYDOLLARSIGN.concat(String.valueOf(the_amount)));

	}
}
